
from Products.Archetypes import atapi
from Products.PloneFormGen.content import fieldsBase
from Products.PloneFormGen.content import fields
from Products.CMFCore.permissions import View

from config import PROJECTNAME
from widget import CaptchaWidget


class FGCaptchaField(fields.FGStringField):

    schema = fieldsBase.BaseFieldSchemaStringDefault.copy() + atapi.Schema((
        fieldsBase.maxlengthField,
        fieldsBase.sizeField,    
    ))

    #######################
    # Every form field must have an fgField attribute that is a field
    # definition created outside of the normal schema.
    # Since this needs to be an instance attribute, we'll set it up
    # in the __init__ method.
    
    def __init__(self, oid, **kwargs):
        """ initialize class """

        fieldsBase.BaseFormField.__init__(self, oid, **kwargs)

        # set a preconfigured field as an instance attribute
        self.fgField = atapi.StringField('fg_string_field',
            searchable = 0,
            required = 1,
            write_permission = View,
            validators = ('isCorrectCaptcha',),
            widget = CaptchaWidget(),
            )
    
atapi.registerType(FGCaptchaField, PROJECTNAME)